<template>
  <el-card>
    <template #header>
      <h2>客户【{{ customerName }}】的护理记录</h2>
    </template>

    <!-- 时间范围筛选：两个独立的日期 -->
    <el-form :inline="true" class="toolbar">
      <el-form-item label="开始日期">
        <el-date-picker
          v-model="filters.startDate"
          type="date"
          placeholder="开始日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="结束日期">
        <el-date-picker
          v-model="filters.endDate"
          type="date"
          placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 护理记录表格 -->
    <el-table :data="records" stripe style="margin-top:16px;">
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="staffName" label="护理员姓名" width="120" />
      <el-table-column prop="staffPhone" label="联系方式" width="120" />
      <el-table-column prop="staffEmail" label="邮箱" width="180" />
      <el-table-column prop="projectName" label="项目名称" width="100" />
      <el-table-column prop="projectPrice" label="项目价格" width="100" />
      <el-table-column prop="quantity" label="数量" width="80" />
      <el-table-column prop="content" label="护理内容" />
		<el-table-column prop="careTime" label="护理时间" width="150"/>
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <el-button
            type="danger"
            size="small"
            @click="removeRecord(row.id)"
          >隐藏</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="page.current"
      :page-size="page.size"
      :total="page.total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
      background
      layout="prev, pager, next, jumper, ->, total"
      style="margin-top:16px; text-align:right;"
    />

    <!-- 返回按钮 -->
    <el-button style="margin-top:16px;" @click="goBack">返回客户列表</el-button>
  </el-card>
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'

const axios = inject('axios')
const route = useRoute()
const router = useRouter()

// 从 URL query 拿到 customerId 和 customerName
const customerId = Number(route.query.customerId || 0)
const customerName = route.query.name || ''

// 绑定开始/结束日期
const filters = reactive({ startDate: '', endDate: '' })

// 表格数据 & 分页
const records = ref([])
const page = reactive({ current: 1, size: 10, total: 0 })

// 查询护理记录
async function fetchRecords() {
  const start = filters.startDate ? filters.startDate + ' 00:00:00' : null
  const end = filters.endDate ? filters.endDate + ' 23:59:59' : null
  try {
    const { data: resp } = await axios.get(
      '/CareRecordController/queryByCustomerId',
      { params: { customerId, cur: page.current, number: page.size, start, end } }
    )
    records.value = resp.data
    page.total = resp.total
  } catch (err) {
    ElMessage.error('加载失败')
  }
}

function onSearch() {
  page.current = 1
  fetchRecords()
}

function handlePageChange(val) {
  page.current = val
  fetchRecords()
}

function handleSizeChange(val) {
  page.size = val
  page.current = 1
  fetchRecords()
}

async function removeRecord(id) {
  try {
    await ElMessageBox.confirm('确认隐藏此护理记录？', '提示', { type: 'warning' })
    await axios.get('/CareRecordController/deleteCareRecord', { params: { id } })
    ElMessage.success('已隐藏该记录')
    fetchRecords()
  } catch {}
}

function goBack() {
  router.push({ path: '/CustomerCareRecord' })
}

onMounted(fetchRecords)
</script>

<style scoped>
.toolbar { margin-bottom: 16px; }
</style>
